package com.boruishangge.persagysdk.uitls.EmptyViewUtils;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.boruishangge.persagysdk.R;


/**
 * Created by wangmaobo on 2019/2/21.
 */
public class ShowExceptionUIHelper implements ShowExceptionUIListener {
    //常量
    //网络异常，接口请求失败
    public static final int NETWORK_ERROR = 0;
    //搜索结果为空
    public static final int EMPTY_SEARCH = 1;
    //接口数据为空
    public static final int EMPTY_CONTENT = 2;
    //retry 再次刷新展示有数据的UI
    public static final int NORMAL_CONTENT = 3;
    private Context mContext;
    //传入的view相关参数
    private LayoutInflater mInflater;
    private View mCurrentView;
    private ViewGroup mParentView;
    private int mCurrentViewIndex;
    private ViewGroup.LayoutParams mParams;
    // listener
    private OnExceptionClickListener mNetWorkClickListener;
    //错误页view
    private View mEmptyView;
    private ImageView mEmptyImageView;
    private TextView mEmptyTextView;
    private TextView mEmptyRetryView;
    private OnNetWorkRetryListener mRetryListener;

    public ShowExceptionUIHelper(Context context) {
        mContext = context;
    }

    private void showView(View view) {
        mEmptyView.setVisibility(View.VISIBLE);
        if (mParentView.getChildAt(mCurrentViewIndex) != view) {
            ViewGroup viewParent = (ViewGroup) view.getParent();
            if (viewParent != null) {
                viewParent.removeView(view);
            }
            mParentView.removeViewAt(mCurrentViewIndex);
            mParentView.addView(view, mCurrentViewIndex, mParams);
        }
    }

    //为了兼容特殊界面-有缓存的界面，即便网络请求失败不会出现网络请求失败的界面
    private boolean isRealNeedNetworkError = true;

    //为了兼容特殊界面-有缓存的界面，即便网络请求失败不会出现网络请求失败的界面
    public ShowExceptionUIHelper setIsRealNeedNetworkError(boolean isRealNeedNetworkError) {
        this.isRealNeedNetworkError = isRealNeedNetworkError;
        return this;
    }

    /**
     * 切换view此步必不可少，根据实际情况还原以前的view布局
     */
    public void resetContentView() {
        showView(mCurrentView);
    }

    public void refreshUI(int type) {
        Log.e("tag", "type=" + type);
        switch (type) {
            case ShowExceptionUIHelper.NETWORK_ERROR:
                  showNoNetWork();
                break;
            case ShowExceptionUIHelper.EMPTY_SEARCH:
                showNoSearchContent();
                break;
            case ShowExceptionUIHelper.EMPTY_CONTENT:
                showNoContent();
                break;
            case ShowExceptionUIHelper.NORMAL_CONTENT:
                showContent();
                break;
        }
    }

    private String notice_noContent = "页面内容为空";
    private String notice_noNetwork = "网络异常";

    @Override
    public void showNoContent() {
        mEmptyRetryView.setVisibility(View.GONE);
        mEmptyTextView.setText(notice_noContent);
        mEmptyImageView.setImageResource(R.mipmap.empty_page);
        showView(mEmptyView);
    }

    @Override
    public void showNoSearchContent() {

        mEmptyRetryView.setVisibility(View.GONE);
        mEmptyTextView.setText("搜索结果为空");
        mEmptyImageView.setImageResource(R.mipmap.empty_search);
        showView(mEmptyView);
    }

    @Override
    public void showNoNetWork() {
        mEmptyTextView.setText(notice_noNetwork);
        mEmptyRetryView.setVisibility(View.VISIBLE);
        mEmptyImageView.setImageResource(R.mipmap.network_error);
        if (mNetWorkClickListener != null) {
            mEmptyRetryView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mNetWorkClickListener.onClick();
                }
            });
        }
        showView(mEmptyView);
    }

    @Override
    public void showContent() {
        showView(mCurrentView);
        if (null != mRetryListener) {
            mRetryListener.retry();
        }
    }

    /**
     * 根据需求自定义自己的属性，下面的内容已经在构造函数里补充了
     *
     * @param view
     * @return
     */
    public ShowExceptionUIHelper setCurrentView(View view) {
        this.mCurrentView = view;
        init(view);
        return this;
    }

    private void init(View view) {
        mInflater = LayoutInflater.from(mContext);
        mCurrentView = view;
        //default
        mEmptyView = mInflater.inflate(R.layout.common_no_content, null);
        mEmptyImageView = mEmptyView.findViewById(R.id.iv_error);
        mEmptyTextView = mEmptyView.findViewById(R.id.tv_errorMessage);
        mEmptyRetryView = mEmptyView.findViewById(R.id.btn_retry);
        //default
        if (null == mCurrentView) {
            Log.e("tag", "currentView is null");
            mParentView = ((Activity) mContext).getWindow().getDecorView().findViewById(android.R.id.content);
            mCurrentView = mParentView.getChildAt(0);
        } else if (mCurrentView.getParent() != null) {
            Log.e("tag", "currentView is not null");
            mParentView = (ViewGroup) mCurrentView.getParent();
        } else {
            Log.e("tag", "currentView is not null !!");
            mParentView = mCurrentView.getRootView().findViewById(android.R.id.content);
        }
        mParams = mCurrentView.getLayoutParams();
        int count = mParentView.getChildCount();
        for (int i = 0; i < count; i++) {
            if (mCurrentView == mParentView.getChildAt(i)) {
                mCurrentViewIndex = i;
                break;
            }
        }

    }

    public ShowExceptionUIHelper setEmptyView(View view) {
        this.mEmptyView = view;
        return this;
    }

    public ShowExceptionUIHelper setOnNetworkErrorClickListener(OnExceptionClickListener listener) {
        this.mNetWorkClickListener = listener;
        return this;
    }

    public ShowExceptionUIHelper setRetryListener(OnNetWorkRetryListener listener) {
        mRetryListener = listener;
        return this;
    }

    public ShowExceptionUIHelper setNotice_noContent(String content) {
        notice_noContent = content;
        return this;
    }

    public ShowExceptionUIHelper setNotice_noNetwork(String content) {
        notice_noNetwork = content;
        return this;
    }
}
